import {config} from "../../../../config";
import {handlerGobackClick} from "../../../../utils/util";
import User from "../../../../models/user";
const user = new User()

/**
 * Created by sail on 2017/4/14.
 */
Page({
  data: {
    src: '',
    user:{},
  },
  upload () {
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success (res) {
        const src = res.tempFilePaths[0]

        wx.navigateTo({
          url: `../upload/index?src=${src}`
        })
      }
    })
  },
  addEventListener () {
    // weDebug.event.on('avatar:finish', (path) => {
    //   this.setData({
    //     src: path
    //   })
    // })
  },
  removeEventListener () {
    // weDebug.event.off('avatar:finish')
  },
  onLoad (option) {
    let { avatar } = option
    if (avatar) {
      this.setData({
        src: avatar
      })
    }
    this.addEventListener()
  },
  onShow(){
    this.initPageData()
  },
  onUnload (option) {
    this.removeEventListener()
  },
  handlerGobackClick,
  uploadImage(path){
    var _this = this;
    let token = wx.getStorageSync("token")
    wx.showLoading({
      title: '正在上传..',
    });
    wx.uploadFile({
      url: config.baseUrl + "/mini/user/updateHeadImg",
      filePath: path,
      name: "file",
      header: {
        "Content-Type": "multipart/form-data",
        "X-Access-Token": token,
      },
      formData: {
        user: "test",
      },
      success: function (res) {
        var data = JSON.parse(res.data);
        if (data.success !== true) {
          wx.showToast({
            title: data.message,
            duration: 2000,
          });
          return false;
        }
        wx.hideLoading();
        _this.setData({
          src: data.result,
        });
      },
    });
  },
  editNickname(){
    wx.navigateTo({
      url:'/owner-app/pages/one-input/index'
    })
  },
  initPageData(){
    user.getUserInfo()
        .then((res)=>{
          this.setData({
            user:res.result.user,
            src:res.result.user.headimg
          })
        })
        .catch((err)=>{
          console.log(err);})
  },
  changePhone(){
    wx.navigateTo({
      url:'/owner-app/pages/change-phone/index'
    })
  }
})
